package com.bos.prop.controller;

import com.bos.dao.*;
import com.bos.domain.Kabupaten;
import com.bos.domain.Periode;
import com.bos.editor.KabupatenEditor;
import com.bos.editor.PeriodeEditor;
import com.bos.model.Bos02Request;
import com.bos.model.DanaBosKecamatanRequest;
import com.bos.model.DayaSerapRequest;
import com.bos.report.factory.Bos02Factory;
import com.bos.report.factory.DanaBosKecamatanFactory;
import com.bos.report.factory.DayaSerapFactory;
import com.bos.util.UrlUtil;
import com.bos.util.enums.JenjangSekolah;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 5/22/11
 * Time: 10:57 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ReportContoller {

    private JumlahSiswaDAO jumlahSiswaDAO;
    private AnggaranDAO anggaranDAO;
    private PeriodeDAO periodeDAO;
    private TahunDAO tahunDAO;
    private KabupatenDAO kabupatenDAO;

    private Bos02Factory bos02Factory;
    private DayaSerapFactory dayaSerapFactory;
    private DanaBosKecamatanFactory danaBosKecamatanFactory;
    private RestTemplate restTemplate;
    /*private Credentials credentials;*/


    /* --------------------- Getter and Setter ---------------------*/

    /*public Credentials getCredentials() {
        return credentials;
    }

    @Autowired
    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
        if (restTemplate != null) {
            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
            HttpClient client = factory.getHttpClient();
            client.getState().setCredentials(AuthScope.ANY, credentials);
        }
    }*/

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

//        if (credentials != null) {
//            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
//            HttpClient client = factory.getHttpClient();
//            client.getState().setCredentials(AuthScope.ANY, credentials);
//        }
    }

    @Autowired
    public void setJumlahSiswaDAO(JumlahSiswaDAO jumlahSiswaDAO) {
        this.jumlahSiswaDAO = jumlahSiswaDAO;
    }

    @Autowired
    public void setAnggaranDAO(AnggaranDAO anggaranDAO) {
        this.anggaranDAO = anggaranDAO;
    }

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setBos02Factory(Bos02Factory bos02Factory) {
        this.bos02Factory = bos02Factory;
    }

    @Autowired
    public void setDayaSerapFactory(DayaSerapFactory dayaSerapFactory) {
        this.dayaSerapFactory = dayaSerapFactory;
    }

    @Autowired
    public void setDanaBosKecamatanFactory(DanaBosKecamatanFactory danaBosKecamatanFactory) {
        this.danaBosKecamatanFactory = danaBosKecamatanFactory;
    }

    @Autowired
    public void setTahunDAO(TahunDAO tahunDAO) {
        this.tahunDAO = tahunDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    /**
     * ************** METHOD CONTOLLER **********************************************************************************
     */

    @RequestMapping(value = "/report/bos02/new", method = RequestMethod.GET)
    public String createBos02(ModelMap modelMap) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("bos02Request", new Bos02Request());
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("kabupatenList", kabupatenDAO.getAll());
        return "report/bos02/create";
    }

    @RequestMapping(value = "/report/bos02/pdf", method = RequestMethod.POST)
    public ModelAndView downloadBos02(ModelAndView modelAndView, @ModelAttribute("bos02Request") Bos02Request bos02Request) {
        if (bos02Request == null || bos02Request.getJenjangSekolah() == null || bos02Request.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";
        HttpEntity<Bos02Request> httpEntity = new HttpEntity<Bos02Request>(bos02Request);

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(bos02Request.getKabupaten()) + "/report/bos02.json";
        try {
            ResponseEntity<Map> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.POST, httpEntity, Map.class);
            Map response = (Map) responseEntity.getBody();

            //siapkan ModelAndView yang diisi parameter
            bos02Factory.putAllResponseParameter(response);
            modelAndView = new ModelAndView("bos02Report", bos02Factory.getResponseParameter());
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }

        return modelAndView;
    }

    @RequestMapping(value = "/report/dayaSerap/new", method = RequestMethod.GET)
    public String createDayaSerap(ModelMap modelMap) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("dayaSerapRequest", new DayaSerapRequest());
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("kabupatenList", kabupatenDAO.getAll());
        return "report/dayaSerap/create";
    }

    @RequestMapping(value = "/report/dayaSerap/pdf", method = RequestMethod.POST)
    public ModelAndView downloadBos02(ModelAndView modelAndView, @ModelAttribute("dayaSerapRequest") DayaSerapRequest dayaSerapRequest) {
        if (dayaSerapRequest == null || dayaSerapRequest.getJenjangSekolah() == null || dayaSerapRequest.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";
        HttpEntity<DayaSerapRequest> httpEntity = new HttpEntity<DayaSerapRequest>(dayaSerapRequest);

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(dayaSerapRequest.getKabupaten()) + "report/dayaSerap.json";
        try {
            ResponseEntity<Map> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.POST, httpEntity, Map.class);
            Map response = (Map) responseEntity.getBody();

            //siapkan ModelAndView yang diisi parameter
            dayaSerapFactory.putAllResponseParameter(response);
            modelAndView = new ModelAndView("dayaSerapReport", dayaSerapFactory.getResponseParameter());
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }

        return modelAndView;
    }

    @RequestMapping(value = "/report/chart/danaBosKecamatan/new", method = RequestMethod.GET)
    public String createChartDanaBosKecamatan(ModelMap modelMap) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("danaBosKecamatanRequest", new DanaBosKecamatanRequest());

        //nilai untuk global varible chart
        modelMap.addAttribute("chartTitle", "");

        return "report/chart/danaBosKecamatan/create";
    }

    @RequestMapping(value = "/report/chart/danaBosKecamatan/new", method = RequestMethod.POST)
    public String createChartDanaBosKecamatan(ModelMap modelMap, @ModelAttribute("danaBosKecamatanRequest") DanaBosKecamatanRequest danaBosKecamatanRequest) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        if (danaBosKecamatanRequest == null || danaBosKecamatanRequest.getJenjangSekolah() == null || danaBosKecamatanRequest.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        modelMap.addAttribute("jenjangSekolahList", Arrays.asList(JenjangSekolah.values()));
        modelMap.addAttribute("tahunList", tahunDAO.getAll());
        modelMap.addAttribute("danaBosKecamatanRequest", new DanaBosKecamatanRequest());

        //nilai untuk global varible chart
        modelMap.addAttribute("chartTitle", danaBosKecamatanRequest.getPeriode().getTriwulan().getTriwulan() + " " + danaBosKecamatanRequest.getPeriode().getTahun().getTahun() + " (" + danaBosKecamatanRequest.getJenjangSekolah() + ")");
        modelMap.addAttribute("chartKabupaten", danaBosKecamatanRequest.getKabupaten());
        modelMap.addAttribute("chartJenjangSekolah", danaBosKecamatanRequest.getJenjangSekolah());
        modelMap.addAttribute("chartPeriode", danaBosKecamatanRequest.getPeriode());

        return "report/chart/danaBosKecamatan/create";
    }

    /**
     * ************** OTHER METHOD **************************************************************************************
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }
}

